$(function () {
    var cateMenuSwiper = new Swiper('.department-container .cate-menu .gallery-thumbs', {});
    var galleryTop = new Swiper('.cate-menu-content .gallery-top', {});
    //获取左侧栏目菜单总数
    var cateMenuCount = $('.department-container .cate-menu .swiper-wrapper > div').length;
    // 监听窗口大小变化
    function resize() {
        // 屏幕宽度
        var screenWidth = window.innerWidth;
        // 是否为小于860的屏幕
        var smallScreen = screenWidth <= 860;
        //如果屏幕小于860px
        if (smallScreen) {
            //如果系部概况侧边栏目录滑动容器对象已存在，则销毁
            if (cateMenuSwiper.initialized != undefined) cateMenuSwiper.destroy(true);
            //创建系部概况侧边栏目录滑动容器对象
            cateMenuSwiper = new Swiper('.department-container .cate-menu .gallery-thumbs', {
                direction: 'horizontal',
                slidesPerView: cateMenuCount,
                setWrapperSize :true,
                freeMode: true,
//              autoHeight: true, //高度随内容变化

                width: undefined, //高度随内容变化
                watchSlidesProgress : true,
                watchSlidesVisibility : true,
                observer: true,//修改swiper自己或子元素时，自动初始化swiper
                observeParents: true,//修改swiper的父元素时，自动初始化swiper
                preventLinksPropagation: false,//阻止滑动时触发点击事件
                touchStartPreventDefault: false,
                navigation: {
                    nextEl: '.right-more',
                    prevEl: '.swiper-button-prev',
                },
                breakpoints: {
                    860: {
                        slidesPerView: cateMenuCount > 7 ? cateMenuCount - 2 : cateMenuCount,

                    },
                    660: {
                        slidesPerView: cateMenuCount > 7 ? cateMenuCount - 3 : cateMenuCount,
                    },
                    540: {
                        slidesPerView: cateMenuCount > 7 ? cateMenuCount - 4 : (cateMenuCount > 5 ? cateMenuCount - 2 : cateMenuCount),
                    },
                    375: {
                        slidesPerView: cateMenuCount > 7 ? cateMenuCount - 5 : (cateMenuCount > 5 ? cateMenuCount - 3 : cateMenuCount),
                    }
                }
            });
            cateMenuSwiper.$el.find('.swiper-slide-thumb-active').addClass('item-active');
            //如果菜单栏可视的条目小于总条目数，则显示更多小箭头
            if (cateMenuSwiper.slidesPerView < cateMenuCount && cateMenuSwiper.isBeginning) {
                $('.department-container .cate-menu .right-more').css('display', 'block');
            } else if (cateMenuSwiper.isEnd || cateMenuSwiper.slidesPerView >= cateMenuCount) {
                $('.department-container .cate-menu .right-more').css('display', 'none');
            }
            //侧边栏内容详情
            if (galleryTop.initialized != undefined) galleryTop.destroy(true);
            galleryTop = new Swiper('.cate-menu-content .gallery-top', {
                spaceBetween: 20,
                slidesPerView: 1,
                setWrapperSize :true,
                autoHeight: true, //高度随内容变化
                watchSlidesProgress : true,
                watchSlidesVisibility : true,
                hashNavigation: {
                    watchState: true,
                },
                observer: true,//修改swiper自己或子元素时，自动初始化swiper
                observeParents: true,//修改swiper的父元素时，自动初始化swiper
                on: {
                    init:function(){
                        $(".gallery-thumbs .item-active").removeClass('item-active');
                        $(".gallery-thumbs > div").find('.swiper-slide-thumb-active').addClass('item-active');
                    },
                    slideChangeTransitionStart: function () {
                        $(".gallery-thumbs .item-active").removeClass('item-active');
                        $(".gallery-thumbs > div > div").eq(galleryTop.activeIndex).addClass('item-active');
                    }
                },
                thumbs: {
                    swiper: cateMenuSwiper
                }
            });

        } else { //如果屏幕大于860px，则执行相关操作
            //系部概况侧边栏目录
            if (cateMenuSwiper.initialized != undefined) cateMenuSwiper.destroy(true);

            cateMenuSwiper = new Swiper('.department-container .cate-menu .gallery-thumbs', {
                direction: 'vertical',
                slidesPerView: cateMenuCount,
                setWrapperSize :true,
                freeMode: true,
                // height: window.innerHeight,
                hashNavigation: true,
                preventLinksPropagation: false,//阻止滑动时触发点击事件
                watchSlidesProgress : true,
                watchSlidesVisibility : true,
                observer: true,//修改swiper自己或子元素时，自动初始化swiper
                observeParents: true,//修改swiper的父元素时，自动初始化swiper
            });
            //设置侧边栏菜单高度
            $(cateMenuSwiper.$wrapperEl[0].children).each(function (index,item) {
                cateMenuSwiper.$el.css('height',item.offsetHeight*(index + 1)+ 'px')
            })
            // $('.gallery-thumbs .swiper-slide').width(100);
            // $('.gallery-thumbs .swiper-slide').height("auto");
            //部概况侧边栏目录内容详情
            if (galleryTop.initialized != undefined) galleryTop.destroy(true);
            galleryTop = new Swiper('.cate-menu-content .gallery-top', {
                slidesPerView: 1,
                spaceBetween: 20,
                setWrapperSize: true,
                preloadImages:true,
                autoHeight:true,
                watchSlidesProgress : true,
                watchSlidesVisibility : true,
                hashNavigation: true,
                observer: true,//修改swiper自己或子元素时，自动初始化swiper
                observeParents: true,//修改swiper的父元素时，自动初始化swiper
                on: {
                    // transitionStart: function () {
                    //     $(".gallery-thumbs > div > div").eq(this.activeIndex).click();
                    //     this.update();
                    // },
                    // swiper从当前slide开始过渡到另一个slide时执行，并给当前状态的slide添加状态类样式
                    slideChangeTransitionStart: function () {
                        $(".gallery-thumbs .item-active").removeClass('item-active');
                        $(".gallery-thumbs > div > div").eq(this.activeIndex).addClass('item-active');
                    }
                },
                thumbs: {
                    swiper: cateMenuSwiper
                }
            });

        }
    }

     var query = getUrlParams().query;
     //默认给第一个添加item-active状态用样式
     !$.isEmptyObject(query) || $('.cate-menu div.swiper-slide:first').addClass('item-active').siblings().removeClass('item-active');

    $(window).on('resize', resize).trigger('resize');

});